package com.example.mymarket.Fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.example.mymarket.Data_model.CatalogData;
import com.example.mymarket.Data_model.ResponseData;
import com.example.mymarket.Data_model.categoryList;
import com.example.mymarket.Menu.LeftMenu;
import com.example.mymarket.R;
import com.example.mymarket.adapter.LeftMenuAdapter;
import com.example.mymarket.api.ApiService;
import com.example.mymarket.interfaces.ResponseCallback;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CatalogFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CatalogFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public CatalogFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CatalogFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CatalogFragment newInstance(String param1, String param2) {
        CatalogFragment fragment = new CatalogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    private List<LeftMenu> leftMenus = new ArrayList<>();
    private List<categoryList> catalogList;
    private ListView catalog_left;
    private LeftMenuAdapter leftMenuAdapter;
    private FrameLayout frameLayout;
    private List<Fragment> fragmentList = new ArrayList<>();
//    private List<List<currentSubCategory>>currentSubCategoryList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 加载Fragment的布局文件，替换成你实际的布局名
        View rootView = inflater.inflate(R.layout.fragment_catalog, container, false);
        // 从根视图中查找控件，赋值给成员变量
        catalog_left = rootView.findViewById(R.id.catalog_left);

        ApiService.GetCatalog(new ResponseCallback<ResponseData<CatalogData>>() {
            @Override
            public void onSuccess(ResponseData<CatalogData> result) {
                catalogList = result.getData().getCategoryList();
                if (catalogList != null){
                    initLeftMenu(catalogList);
                    addFragment();
                    selectItem(0);
                } else {
                    Log.e("TAG", "没有数据 " );
                }
            }
        });


        return rootView;
    }

    public void  initLeftMenu(List<categoryList> catalogList){

        for (int i = 0; i < catalogList.size(); i++) {
            leftMenus.add(new LeftMenu(catalogList.get(i).getId(), catalogList.get(i).getName(), false));
        }

        leftMenuAdapter = new LeftMenuAdapter(requireContext(), leftMenus);
        catalog_left.setAdapter(leftMenuAdapter);
        catalog_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });

    }

    private void selectItem(int position) {//切换视图
        FragmentTransaction fragmentTransaction = requireActivity().getSupportFragmentManager().beginTransaction();
        Log.e("TAG", "切换视图: " + this.leftMenus.get(position).toString() + "下标:" + position);
        for (int i = 0; i < leftMenus.size(); i++) {
            leftMenus.get(i).setChecked(false);
            fragmentTransaction.hide(fragmentList.get(i));
        }
        this.leftMenus.get(position).setChecked(true);
        fragmentTransaction.show(fragmentList.get(position));
        fragmentTransaction.commit();
        leftMenuAdapter.notifyDataSetChanged();
    }

    public void addFragment(){//添加碎片的逻辑
        FragmentTransaction fragmentTransaction = requireActivity().getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < leftMenus.size(); i++) {
            CatalogContentFragment catalogContentFragment = new CatalogContentFragment();

            Bundle bundle = new Bundle();
            bundle.putInt("id", catalogList.get(i).getId());
            bundle.putString("name", String.valueOf(catalogList.get(i).getName()));
            bundle.putString("desc", String.valueOf(catalogList.get(i).getDesc()));
            bundle.putString("picUrl",String.valueOf(catalogList.get(i).getPicUrl()));
//            bundle.putByteArray("currentSubCategoryList",currentSubCategoryList);


            catalogContentFragment.setArguments(bundle);

            fragmentList.add(catalogContentFragment);
            fragmentTransaction.add(R.id.catalog_right, fragmentList.get(i));
        }
        fragmentTransaction.commit();
    }
}